package snaptest.core;

import java.io.IOException;

import snaptest.repo.FilePath;
import snaptest.repo.FileRepo;
import snaptest.repo.RepoManager;

public class ReferenceFileLocator {

	private final String filePath;
	
	private FileRepo refRepo = RepoManager.getReferenceRepo();
	
	private FileRepo tmpRepo = RepoManager.getTemporaryRepo();

	public ReferenceFileLocator(Class<?> testClass, String testName) {
		this.filePath = formatFileName(testClass, testName);
	}
	
	static String formatFilePath(Class<?> testClass) {
		return testClass.getPackage().getName().replace('.', '/') + "/" //
				+ testClass.getSimpleName();
	}

	static String formatFileName(Class<?> testClass, String testName) {
		String filePath = formatFilePath(testClass);
		return filePath + "/" + testName + ".txt";
	}

	public boolean referenceExist() {
		return refRepo.exist(filePath);
	}

	public String loadReference() throws IOException {
		return refRepo.load(filePath);
	}

	public void saveActual(String content) throws IOException {
		tmpRepo.save(filePath, content);
	}

	public String getFileName() {
		return new FilePath(filePath).getLastElement();
	}
	
	void setRefRepo(FileRepo refRepo) {
		this.refRepo = refRepo;
	}

	void setTmpRepo(FileRepo tmpRepo) {
		this.tmpRepo = tmpRepo;
	}
}
